var TaskInfo = {
	grid: '#taskinfo-grid',
	form: '#taskinfoForm',

	init: function(){
		//初始化Grid列表数据
		this.initGrid();
	},

	//添加模块
	addTaskInfo: function(){
		var d = this.createTaskInfoDialog();
		d.dialog({title: "Add Job"}).dialog('open');
	},

	//修改模块弹出修改框
	updateTaskInfo: function() {
		var message = Ext.getSingleSelected(this.grid);
		if(message){
			return Ext.alert(message);
		}

		this.update();
	},

	//执行更新操作
	update: function() {
		var record = Ext.getRecord(TaskInfo.grid);
		var d = this.createTaskInfoDialog();
		d.dialog({title: "Edit Job"}).dialog('open');

		//加载form表单
		$(this.form).form('load', record);
		// $('#taskinfoForm input[name=id]').val(1);
	},

	//删除模块
	deleteTaskInfo: function(){
		var message = Ext.getSingleSelected(this.grid);
		if(message){
			return Ext.alert(message);
		}

		Ext.confirm('您确认要删除这条记录吗?', function(){
			Ext.progress('Loading...');
			var record = Ext.getRecord(TaskInfo.grid);
			$.get("/delete/"+record.jobName+"/"+record.jobGroup, function(result){
				if(result.errorCode==0){
					$(TaskInfo.grid).datagrid("reload")
				}else{
                    $(TaskInfo.grid).datagrid("reload")
					Ext.alert(result.message);
				}
				Ext.progressClose();
			});
		});
	},

	//模块详情
	detailTaskInfo: function(index){
		$(TaskInfo.grid).datagrid('selectRow',index);
		var row = Ext.getRecord(this.grid);
		if(!row){return;}

		//弹出Dialog, 并修改Title和隐藏Button
		var d = this.createTaskInfoDialog();
		d.dialog({title: "Detail Job"}).dialog('open');
		$(".dialog-button a").eq(0).hide();
		$(this.form).form('load', row)
	},

	//保存模块
	saveTaskInfo: function(){
		$(TaskInfo.form).form('submit',{
	        url: '/add',
	        onSubmit: function(){
	        	var flag = $(this).form('enableValidation').form('validate');
	        	if(flag) {
	        		Ext.progress('Loading...');
	        	}
	            return flag;
	        },
	        success: function(result){
	        	result = $.parseJSON(result);
	            if(result.code ==200){
	            	var d = TaskInfo.createTaskInfoDialog();
	            	d.dialog('close');
	            	$(TaskInfo.grid).datagrid("reload");
	            }else{
	            	Ext.alert(result.msg);
	            }

	            Ext.progressClose();
	        }
	    });
	},

	//禁用任务
    updUnUseTaskInfo: function(index) {
		$(TaskInfo.grid).datagrid('selectRow',index);
		var row = Ext.getRecord(this.grid);
		if(!row){return;}

		Ext.confirm('您确认要禁用该任务吗?', function(){
			Ext.progress('正在禁用任务...');
			var record = Ext.getRecord(TaskInfo.grid);
			$.post("/updUnUseStatus", {jobId:record.id},function(result){
				if(result.code==0){
					$(TaskInfo.grid).datagrid("reload")
				}else{
					Ext.alert(result.msg);
				}
				Ext.progressClose();
			});
		});
	},

	//开始
    updUseTaskInfo: function(index) {
		$(TaskInfo.grid).datagrid('selectRow',index);
		var row = Ext.getRecord(this.grid);
		if(!row){return;}

		Ext.confirm('您确认要重新开始该任务吗?', function(){
			Ext.progress('正在开始任务...');
			var record = Ext.getRecord(TaskInfo.grid);
            $.post("/updUseStatus",  {jobId:record.id},function(result){
				if(result.code==0){
					$(TaskInfo.grid).datagrid("reload")
				}else{
					Ext.alert(result.msg);
				}
				Ext.progressClose();
			});
		});
	},
    //初始化
    updStautsToUpTaskInfo: function(index) {
        $(TaskInfo.grid).datagrid('selectRow',index);
        var row = Ext.getRecord(this.grid);
        if(!row){return;}
        Ext.confirm('您确认要重新初始化该任务吗?', function(){
            Ext.progress('正在初始化任务...');
            var record = Ext.getRecord(TaskInfo.grid);
            $.get("/updStautsToUp/"+record.jobGroup+"/"+record.jobName, function(result){
                if(result.code==0){
                    $(TaskInfo.grid).datagrid("reload")
                }else{
                    Ext.alert(result.msg);
                }
                Ext.progressClose();
            });
        });
    },

	//初始化表单
	initGrid: function(){
		$(this.grid).datagrid({
			toolbar: '#taskinfo-tbar',
		    url:'/queryList?pageNum=1&pageSize=1',
		    method: 'post',
		    fitColumns: true,
		    striped: true,
		    fit: true,
		    pagination:false,
	        rownumbers:true,
	        ctrlSelect: false,
	        singleSelect: true,
	        border: false,
		    columns:[[
				{field:'id',title:'',checkbox:'true', width:20},
				{field:'jobName',title:'JobName',width:60,formatter:function(v,r,i){return '<a href="javascript:void(0)" onclick="TaskInfo.detailTaskInfo('+i+')">'+v+'</a>';}},
				{field:'jobGroup',title:'JobGroup',width:50},
                {field:'clazz',title:'clazz',width:50},
				{field:'description',title:'description',width:60},
                {field:'status',title:'status',width:30,formatter:function(v,r,i){
                    var val = '';
                    if(r.status == '1') {
                        val = '正常'
                    }else if(r.status == '0'){
                        val = '禁用'
                    }
                    return val;
                }
                },
                {field:'runPoolCount',title:'RunPoolCount',width:20},
				{field:'cronExpression',title:'CronExpression',width:40},
				{field:'createTime',title:'CreateTime',width:40},
                {field:'Opr',title:'Opr',width:40,formatter:function(v,r,i){
                    var val = '';
                    if(r.status == '1') {
                        val += '<a href="javascript:void(0)" onclick="TaskInfo.updUnUseTaskInfo('+i+')">禁用</a>'
                        val += '<a href="javascript:void(0)" style="margin-left: 15px" onclick="TaskInfo.updStautsToUpTaskInfo('+i+')">初始化状态</a>'
                    }else if(r.status == '0'){
                        val += '<a href="javascript:void(0)" onclick="TaskInfo.updUseTaskInfo('+i+')">启用</a>'
                    }
                    return val;
                }
                }
		    ]]
		});
	},

	//模块框
	createTaskInfoDialog: function() {
		$(this.form).form('clear');
		$('#taskinfoForm input[name=id]').val(0);

		var d = $('#taskinfoDialog').dialog({
		    width:500,
		    minimizable: false,
		    maximizable: false,
		    collapsible: false,
		    resizable: false,
		    modal: true,
		    iconCls: 'icon-win',
		    buttons: [
		       {text: 'Save', handler: this.saveTaskInfo},
		       {text: 'Close', handler: function(){d.dialog('close');}}
		    ]
		});
		return d;
	}
}
